
var GlobalData = require('../../../../utils/GlobalData.js');
var dataManager = require('../../../../utils/dataManager.js');
var app = getApp();

Page({

  data: {
    homeData: Object,
    swiperCurrent: 0,
    indicatorDots: false,
    autoplay: true,
    interval: 3000,
    duration: 800,
    circular: true,
    mark: 0,
    newmark: 0,
    open: false,
    istoright: true,
    levels: "折扣",
    imgUrls: [
      ""
    ],
    token: "",
    hidden: true,
    isDistribution1: false,
    isDistribution2: false,
    isShowRecharge: false,
    isShowSaleCard: true,
    styleCss: "home_chongzhikuang1",
    kakuImg: "kaku1",
    starArr: [],
    isShowIntegral: false,
    isshowiphonex: false,
  },
  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function (options) {

    this.getRecharge();
  },
  // 获取是否显示直充
  getRecharge: function () {
    var that = this;
    var img = "";
    var url = app.getRequestUrl(app.getConfig().isFormal);
    if (!GlobalData.Homeimgurls.length){
      wx.request({
        url: url,
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        method: "POST",
        data: Util.json2Form({ method: "getslidelist", "token": app.getToken(), "gameid": 100, "pageindex": 1, "pagesize": 100 }),
        complete: function (res) {
          let pkg = res;
          let arr = [];
          for (let i = 0; i < res.data.data.length; i++) {
            arr.push(res.data.data[i].picture)
          }
          GlobalData.Homeimgurls = arr
          that.setData({
            imgUrls: GlobalData.Homeimgurls
          })
        }
      })
    }
    
    // lunboHead.png
    // if (app.getConfig().isShowRecharge) {
    //   img = "kaku1";
    //   that.data.imgUrls = [
    //     "https://shop.queyouquan.cn/Upload/Slide/636452137061639361_lunbo.png"
    //   ]
    // }
    // else {
    //   img = "haibao1"
    //   that.data.imgUrls = [
    //     "/images/haibaoImg/lunboHead.png"
    //   ]
    // }
    that.setData({
      isShowRecharge: app.getConfig().isShowRecharge,
      kakuImg: img,
      imgUrls: GlobalData.Homeimgurls
    })

  },
  onShow: function () {
    

    that = this;
    if (GlobalData.isshowiphone){
      that.setData({
        isshowiphonex:true
      })
    }else{
      that.setData({
        isshowiphonex: false
      })
    }
    if (app.getConfig().isShowIntegral) {
      this.setData({
        isShowIntegral: app.getConfig().isShowIntegral
      })
    }
    // console.log(this.data.isShowIntegral)
    // this.getRecharge();
    if (GlobalData.homeData && GlobalData.homeData.isopendistribution) {

      if (!GlobalData.homeData.isopendistribution || (GlobalData.homeData.isopendistribution && (GlobalData.homeData.isdefaultlevels && !GlobalData.homeData.isdefaultshare))) {
        this.setData({
          isDistribution: false
        })
      }
    }

    dataManager.removeBtnObserver("home")
    this.setData({
      hidden: true
    })
    if (app.getToken() == "" || app.getToken() == undefined || app.getToken() == null) {
      app.onTip("token已失效，请重新登录。", '/pages/login/login', true);
      return;
    } else {
      GlobalData.token = app.getToken();
    };
    /**获取用户信息 */
    let url = app.getRequestUrl(app.getConfig().isFormal);
    wx.request({
      url: url,
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      data: Util.json2Form({ method: "getagent", token: app.getToken() }),
      complete: function (res) {
        let pkg = res.data;
        if (pkg && pkg.errcode == 0) {
          let temp = "";
          if (pkg.data.discount_card >= 1) {
            temp = "无折扣";
          } else {
            temp = "当前享受" + (pkg.data.discount_card * 10) + "折";
          }
          // pkg.data.isopensalecard = 0;
          if (pkg.data.isopensalecard) {
            that.setData({
              isShowSaleCard: true
            })
          } else {
            that.setData({
              isShowSaleCard: false
            })
          }
          that.setData({
            homeData: pkg.data,
            levels: temp
          });
          GlobalData.homeData = pkg.data;
          // GlobalData.homeData.isopensalecard = 0;
          that.onComplete();
        } else {
          app.show(pkg.errmsg)
        }

      }
    })


  },
  // 获取
  /**处理数据 */
  onProcessData: function (data) {
  },
  /**
   *  // 生命周期函数--监听页面初次渲染完成
   */
  onReady: function (options) {

  },

  /**获取首页信息完成之后 新的获取幻灯片数据 */
  onComplete: function () {
    if (GlobalData.homeData) {
      this.data.homeData = GlobalData.homeData;
      let arrstar = [];
      if (this.data.homeData.BPlevel > 0)
        for (let i = 0; i < this.data.homeData.BPlevel; i++) {
          arrstar.push(i);
        }
      this.setData({
        starArr: arrstar,
        homeData: this.data.homeData
      })
      // GlobalData.homeData.isopendistribution = 0
      // 判断是否是或者有没有开通三级分销
      if (!GlobalData.homeData.isopendistribution || (GlobalData.homeData.isopendistribution && (GlobalData.homeData.isdefaultlevels && !GlobalData.homeData.isdefaultshare))) {
        this.setData({
          isDistribution1: false,
          isDistribution2: true,
          
        })
      }else{
        this.setData({
          isDistribution2: false,
          isDistribution1: true,

        })
      }


      //新的获取幻灯片数据
      // wx.request({
      //   url: app.getConfig().url,
      //   header: {
      //     "Content-Type": "application/x-www-form-urlencoded"
      //   },
      //   method: "POST",
      //   data: Util.json2Form({ method: "getslidelist", token: GlobalData.token, "gameid": 100, "pageindex": 1, "pagesize": 100 }),
      //   complete: function (res) {
      //     let pkg = res.data;
      //     if (pkg && pkg.errcode == 0) {
      //       var arrPic = [];
      //       for (let i = 0; i < pkg.data.length; i++) {
      //         arrPic.push(pkg.data[i].picture);
      //       }
      //       that.setData({
      //         // imgUrls: arrPic
      //       });
      //     } else {
      //       app.onTip(pkg.errmsg)
      //       return;
      //     }
      //   }
      // })
    }
    else {
      app.onTip("token已失效，请重新登录。", '/pages/login/login', true);
      return;
    }
  },
  /**转账 */
  onTransfer: function () {
    this.setData({
      hidden: false
    })
    dataManager.UseNavigateTo('../../homes/transfer/transfer', false);
    // dataManager.UseNavigateTo("/pages/template/showagent/showagent", false);

  },
  /**提现 */
  onWithdraw: function () {

    if (this.data.homeData && this.data.homeData.idcard) {
      this.setData({
        hidden: false
      })
      /**绑定银行信息 */
      dataManager.OnRequestPost({ method: "getbankaccountlist", "token": GlobalData.token }, this.onWithdrawComplete, false, this.onError);
    } else {
      wx.showModal({
        title: '提示',
        content: "提现请先完成实名认证",
        confirmText: "实名认证",
        success: function (res) {
          if (res.confirm) {
            dataManager.UseNavigateTo("/pages/authentication/authentication", false)
          }
        }
      })
    }

  },
  onWithdrawComplete(data) {
    GlobalData.bindInfo = data;
    console.log(GlobalData.bindInfo)
    for (let i = 0; i < GlobalData.bindInfo.length; i++) {
      if (GlobalData.bindInfo[i].type == 1) {
        GlobalData.withDrawType = GlobalData.clone(GlobalData.bindInfo[i])
        GlobalData.withDrawType["img"] = "yhk"
      }
    }
    this.setData({
      hidden: true
    })
    dataManager.UseNavigateTo('../../homes/withdraw/withdraw', false);
  },

  /**查看收益明细 */
  goEarningDetail: function () {
    this.setData({
      hidden: false
    })
    dataManager.OnRequestPost({ method: "getpromoterebatelist", "token": GlobalData.token, "pageindex": GlobalData.EarningDetailother["index"], "pagesize": GlobalData.EarningDetailother["size"] }, this.onProcessEarning, false, this.onError);
  },
  onError: function () {
    this.setData({
      hidden: true
    })
  },
  onProcessEarning: function (data) {
    for (let i = 0; i < data.length; i++) {
      GlobalData.EarningDetail[i] = GlobalData.clone(data[i]);
    }
    this.setData({
      hidden: true
    })
    dataManager.UseNavigateTo('../../homes/earningdetail/earningdetail', false);

  },

  /**设置界面 */
  onSetting: function () {
    this.setData({
      hidden: false
    })
    dataManager.UseNavigateTo('../../homes/setting/setting', false);


  },
  /**充值 goMoneyRecharge*/
  goMoneyRecharge: function () {
    this.setData({
      hidden: false
    })
    dataManager.UseNavigateTo('../../homes/purchase/moneyrecharge/moneyrecharge', false);

  },
  /**查看余额明细 */
  goBalanceDetail: function () {
    this.setData({
      hidden: false
    })

    dataManager.OnRequestPost({ method: "getamountlist", "token": GlobalData.token, "pageindex": GlobalData.BalanceDetailother["index"], "pagesize": GlobalData.BalanceDetailother["size"] }, this.onProcessBalance, false, this.onError);

  },
  onProcessBalance: function (data) {
    for (let i = 0; i < data.length; i++) {
      GlobalData.BalanceDetail[i] = GlobalData.clone(data[i]);
    }
    this.setData({
      hidden: true
    })
    dataManager.UseNavigateTo('../../homes/balancedetail/balancedetail', false);

  },
  /**购买房卡 */
  onBuyCard: function () {

    this.setData({
      hidden: false
    })
    dataManager.UseNavigateTo('../../homes/purchase/buycard/buycard', false);


  },
  /**直充 */
  onRecharge: function () {
    this.setData({
      hidden: false
    })
    dataManager.UseNavigateTo('../../homes/purchase/recharge/recharge', false);


  },
  /**俱乐部直充 */
  onClub: function () {
    this.setData({
      hidden: false
    })
    dataManager.UseNavigateTo('../../homes/purchase/club/club', false);
  },
  /**前往卡库 */
  goCardDepots: function () {
    var that = this;
    that.setData({
      hidden: false
    })
    if (that.data.isShowRecharge) {
      dataManager.OnRequestPost({ method: "getagentgame", "token": GlobalData.token }, that.onKindComplete, false, that.onError);
    } else {
      dataManager.UseNavigateTo("/pages/template/entrepreneurship/entrepreneurship", true);
    }


  },
  onKindComplete: function () {
    dataManager.OnRequestPost({ method: "getcdkeycount", "token": GlobalData.token }, this.onCardComplete, false, this.onError);
  },
  onCardComplete: function () {
    this.setData({
      hidden: true
    })
    dataManager.UseNavigateTo('../../carddepots/carddepot/carddepot', true);
  },
  showTxt: function (e) {
    var str = ""
    if (GlobalData.homeData.businessname)
      str = GlobalData.homeData.businessname
    var msg = "添加商务微信" + GlobalData.homeData.businessname + "，即可开通推广功能"
    wx.showModal({
      title: "提示",
      content: msg,
      showCancel: true,
      confirmText: "确认复制",
      success: function (res) {
        if (res.confirm) {
          wx.setClipboardData({
            data: str,
            success() {
              wx.getClipboardData({
                success(res) {
                  // app.show("已复制成功。")
                }
              })
            },
            fail(res) {
            }
          })
        }
      }
    })
  },
  /**前往推广 */
  goPromotion: function () {
    if (!GlobalData.homeData.isopendistribution) {
      app.show("该地区暂未开启推广功能");
      return;
    }
    else if (GlobalData.homeData.isopendistribution && (GlobalData.homeData.isdefaultlevels && !GlobalData.homeData.isdefaultshare)) {
      // this.showTxt("提示", "添加商务微信" + GlobalData.homeData.businessname + "，即可开通推广功能", "确认复制微信号");
      this.showTxt();
      return;
    }
    else {
      this.setData({
        hidden: false
      })
      dataManager.UseNavigateTo('../../promotions/promotion/promotion', true);
    }

  },
  /**前往帮助主页 */
  goHomeHelp: function () {
    this.setData({
      hidden: false
    })
    dataManager.UseNavigateTo('../../homes/help/homehelp/homehelp', false);

  },
  /**前往总推广收益 goAccountBalance */
  goPromotionBalance: function () {
    this.setData({
      hidden: false
    })
    dataManager.UseNavigateTo('../../promotions/promotebalance/promotebalance', false);

  },

  /**前往余额页面 */
  goAccountBalance: function () {
    this.setData({
      hidden: false
    })
    dataManager.OnRequestPost({ method: "getamountlist", "token": GlobalData.token, "pageindex": GlobalData.BalanceDetailother["index"], "pagesize": GlobalData.BalanceDetailother["size"] }, this.onProcessAccount, false, this.onError);

  },
  /**部分余额明细 */
  onProcessAccount: function (data) {
    for (let i = 0; i < data.length; i++) {
      GlobalData.BalanceDetail[i] = GlobalData.clone(data[i]);
    }
    this.setData({
      hidden: true
    })
    dataManager.UseNavigateTo('../../homes/accountbalance/accountbalance', false);

  },
  /**前往海报系统 */
  getShip: function () {
    this.setData({
      hidden: false
    })
    dataManager.UseNavigateTo("/pages/template/entrepreneurship/entrepreneurship", false);
  },
  /**弹框提示是否注销账号 */
  onShowTip: function () {
    var that = this;
    wx.showModal({
      content: "是否注销当前账号？",
      // showCancel: true,
      cancelText: "取消",
      confirmText: "确认",
      success: function (res) {
        //全部取出
        if (res.confirm) {
          that.onLogout();
        }
      }
    });
  },


  /**注销账号00 */
  onLogout: function () {
    let url = app.getRequestUrl(app.getConfig().isFormal);
    wx.request({
      url: url,
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      data: Util.json2Form({ method: "Cancellation", token: app.getToken() }),
      complete: function (res) {
        let pkg = res.data;

        if (pkg && pkg.errcode == 0) {
          GlobalData.phoneNumber = "";
          GlobalData.passWord = "";
          app.setPhone("");
          app.setPassword("");
          GlobalData.homeData = {};
          GlobalData.destroy()
          if (app.getConfig().isShowIntegral) {
            GlobalData.Purge()
          }
          dataManager.UseNavigateTo('/pages/login/login', true);

        } else {
          app.show(pkg.errmsg)
        }
      }
    })



  },
  goMall() {
    dataManager.UseNavigateTo("/pages/integralmall/integralmall", true)
  },
  goMyInteMall() {
    dataManager.UseNavigateTo("/pages/myintegral/myintegral", false)
  },
  //轮播图的切换事件
  swiperChange: function (e) {
    if (e.detail.source == "touch") {
      this.setData({
        swiperCurrent: e.detail.current
      })
    }
  },
  onClickImg: function (e) {
    console.log(e)
  },
  //点击指示点切换
  chuangEvent: function (e) {
    this.setData({
      swiperCurrent: e.currentTarget.id
    })
  },

  tap_ch: function (e) {
    if (this.data.open) {
      this.setData({
        open: false
      });
    } else {
      this.setData({
        open: true
      });
    }
  },
  tap_start: function (e) {
    // touchstart事件
    this.data.mark = this.data.newmark = e.touches[0].pageX;
  },

  tap_drag: function (e) {
    // touchmove事件
    /*
     * 手指从左向右移动
     * @newmark是指移动的最新点的x轴坐标 ， @mark是指原点x轴坐标
     */
    this.data.newmark = e.touches[0].pageX;
    if (this.data.mark < (this.data.newmark - 10)) {
      this.istoright = true;
    }
    /*
     * 手指从右向左移动
     * @newmark是指移动的最新点的x轴坐标 ， @mark是指原点x轴坐标
     */
    if (this.data.mark > (this.data.newmark + 10)) {
      this.istoright = false;

    }
    this.data.mark = this.data.newmark;

  },
  tap_end: function (e) {
    // touchend事件
    this.data.mark = 0;
    this.data.newmark = 0;
    if (this.istoright) {
      this.setData({
        open: true
      });
    } else {
      this.setData({
        open: false
      });
    }
  },
})
var that;
var Util = require('../../../../utils/util.js');